<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备数据</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../../plugins/element-ui/index.css"/>
    <link rel="stylesheet" href="../../styles/common.css"/>
    <link rel="stylesheet" href="../../styles/page.css"/>
</head>
<body>
<div class="dashboard-container" id="cmdData-app">
    <div class="container">
        <div class="tableBar">
            <div class="tableLab">

                <el-date-picker
                        v-model="conditions.date"
                        type="daterange"
                        align="right"
                        unlink-panels
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :picker-options="pickerOptions"
                        format="yyyy-MM-dd"
                        @change="showTime()">
                </el-date-picker>
                <el-button  type="primary" icon="el-icon-search" @click="handleQuery()">搜索</el-button>
            </div>
        </div>
        <el-table  :data="pagination.tableData"  stripe  class="tableBox" width="100%">
            <el-table-column type="index" width="50" label="序号" width="50"></el-table-column>
            <el-table-column prop="tenantId"  label="租户" width="150">
                <template slot-scope="scope">
                    <span v-for="item in tenantList" v-if="scope.row.tenantId==item.id">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="productKey"  label="产品" width="150">
                <template slot-scope="scope">
                    <span v-for="item in productList" v-if="scope.row.productKey==item.productKey">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="deviceKey"  label="设备" width="150">
                <template slot-scope="scope">
                    <span v-for="item in deviceList" v-if="scope.row.deviceKey==item.deviceKey">{{item.name}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="createTime"  label="创建时间" width="180"></el-table-column>

            <el-table-column prop="deviceSubCode"  label="传感器/继电器编号"  width="150"></el-table-column>
            <el-table-column prop="cmdName"  label="指令名称"  width="180"></el-table-column>
            <el-table-column prop="value"  label="指令值"  width="150"></el-table-column>



        </el-table>
        <el-pagination
                class="pageList"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.counts"
                @size-change="handleSizeChange"
                :current-page.sync="pagination.page"
                @current-change="handleCurrentChange"
        ></el-pagination>
    </div>
</div>
<script src="../../plugins/vue/vue.js"></script>
<script src="../../plugins/element-ui/index.js"></script>
<script src="../../plugins/axios/axios.min.js"></script>
<script src="../../js/request.js"></script>
<script src="../../js/index.js"></script>
<script src="../../api/deviceData.js"></script>
<script type="text/javascript">
    new Vue({
        el: '#cmdData-app',
        data() {
            return {
                action: '',
                type: '',
                pagination: {
                    tableData: [],
                    page: 1,
                    pageSize: 10,
                    counts:0,
                },
                conditions:{
                    propertyName:'',
                    date:'',
                    deviceKey:''
                },
                tenantList:[],
                deviceList:[],
                productList:[],
                modelList:[],
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                }
            }
        },
        methods: {
            async init() {
                const params = {
                    page: {
                        current: this.pagination.page,//当前页码
                        size: this.pagination.pageSize,//每页大小
                        startTime:this.conditions.startTime,
                        endTime:this.conditions.endTime
                    },
                    propertyName: this.conditions.propertyName ? this.conditions.propertyName : undefined,
                    deviceKey: this.conditions.deviceKey ? this.conditions.deviceKey : undefined
                };
                await findCmdDataPage(params).then(res => {
                    console.log(params);
                    if (String(res.code) === '200') {
                        this.pagination.tableData = res.data.records;
                        this.pagination.counts = res.data.total;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                })
            },
            handleQuery() {
                this.page = 1;
                this.init();
            },
            // 关闭弹窗
            handleClose(st) {
                this.classData.dialogVisible = false
            },

            //查询所有产品
            async initProductList(){
                await findProductList().then(res => {
                    if (String(res.code) === '200') {
                        this.productList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有设备
            async initDeviceList(){
                await findDeviceList().then(res => {
                    if (String(res.code) === '200') {
                        this.deviceList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有租户
            async initTenantList(){
                await findTenantList().then(res => {
                    if (String(res.code) === '200') {
                        this.tenantList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //查询所有物模型
            async initModelList(){
                await findModelList().then(res => {
                    if (String(res.code) === '200') {
                        this.modelList = res.data;
                    } else {
                        this.$message.error(res.msg || '操作失败')
                    }
                }).catch(err => {
                    this.$message.error('请求出错了：' + err)
                });
            },
            //格式化json数据
            formatter(row, column) {
                console.log(JSON.stringify(row.data));
                // console.log(column);

                return JSON.stringify(row.data);
            },
            //分页相关全部操作
            handleSelectionChange(val) {
                let checkArr = []
                val.forEach((n) => {
                    checkArr.push(n.id)
                })
                this.checkList = checkArr
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.init()
            },
            handleCurrentChange(val) {
                this.page = val
                this.init()
            },
        },
        created() {
            this.conditions.deviceKey = requestUrlParam('deviceKey');
            this.init();
            this.initDeviceList();
            this.initProductList();
            this.initTenantList();
            this.initModelList();
        }
    })
</script>
</body>
</html>